import os
import openai
import requests

openai.api_key = ""

def send(msg):

    if not openai.api_key:
        print("需要导入openai api_key.")
        os.exit(1)

    url = "https://api.openai.com/v1/chat/completions"
    headers = {"Content-Type": "application/json",
               "Authorization": "Bearer {}".format(openai.api_key)}
    data = {
        "model": "gpt-3.5-turbo",
        "messages": [{
            "content": msg,
            "role":"user"
        }]
    }

    response = requests.post(url, headers=headers, json=data)

    json_data = response.json()
    id = json_data["id"]
    t = json_data["created"]
    total_token = json_data["usage"]["total_tokens"]
    role = json_data["choices"][0]["message"]["role"]
    content = json_data["choices"][0]["message"]["content"]

    return "{} . used tokens:{} .".format(content,total_token)


if __name__ == "__main__":
    msg="财务制度是什么意思？\nA:"
    send(msg)